package com.aibank.controller;

        import com.aibank.entity.Recharge;
        import com.aibank.entity.RequestEnity;
        import com.aibank.entity.User;
        import com.aibank.repository.RechargeRepository;
        import com.aibank.service.DirectService;
        import com.aibank.utils.RSAUtils;
        import com.alibaba.fastjson.JSONObject;
        import org.apache.http.HttpResponse;
        import org.apache.http.client.HttpClient;
        import org.apache.http.client.methods.HttpGet;
        import org.apache.http.impl.client.HttpClients;
        import org.apache.http.util.EntityUtils;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.web.bind.annotation.*;

        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import java.io.IOException;
        import java.net.URLEncoder;
        import java.util.HashMap;
        import java.util.Iterator;
        import java.util.Map;

/**
 * 直连接口,开发的
 * 外包装+json
 *          "serviceName":"接口名称",
 *         "platformNo":"平台编号",
 *         "reqData":"业务数据报文",
 *         "keySerial":"证书序号",
 *         "sign":"签名"
 */
@RestController
@RequestMapping("/lanmaotech")
@CrossOrigin
public class DirectController {
    @Value("${key.privateKey}")
    private String privateKey;
    @Value("${key.publicKey}")
    private String publicKey;
    @Autowired
    private DirectService directService;

    //验签
    @RequestMapping("/service")
    public String service(@RequestBody(required = false) RequestEnity requestEnity, HttpServletResponse response) throws Exception {
        // 校验外包装里的签名字段
        String userSign = requestEnity.getSign();
        // 在银行接口用私钥解锁
        String subData = RSAUtils.decrypt(userSign, privateKey);

        String checkData =requestEnity.getReqData().length()>100?requestEnity.getReqData().substring(0,100): requestEnity.getReqData();
        String reqData = requestEnity.getReqData();
        // 把json转成对象
        Map map = JSONObject.parseObject(reqData);

        Map resultMap = new HashMap<>();
        // 验签的处理
        if(!checkData.equals(subData)){
            resultMap.put("requestNo",map.get("requestNo"));//请求流水号
            resultMap.put("code",1);//调用状态
            resultMap.put("status"," INIT");// 业务处理状态
            resultMap.put("errorCode",100003 );//错误码
            resultMap.put("errorMessage","签名验证失败");//错误描述
        }
        // 接口名的处理
        if(requestEnity.getServiceName()==null || requestEnity.getServiceName().trim().equals("")){
            resultMap.put("requestNo",map.get("requestNo"));
            resultMap.put("code",1);
            resultMap.put("status"," INIT");
            resultMap.put("errorCode",100001 );
            resultMap.put("errorMessage","系统错误");
        }
        // 3.4绑定卡还款
        if (requestEnity.getServiceName().equals("DIRECT_RECHARGE")){
            // 还款逻辑
            // 里面装着外包装和reqData
            resultMap= directService.recharge(requestEnity);
            System.out.println("我到百信接口了");
        }

        String json1 = JSONObject.toJSONString(resultMap);
        String json =json1.length()>100?json1.substring(0,100):json1;
        System.out.println(json);
        String sign1 = RSAUtils.encrypt(json, publicKey);
        String sign=sign1.length()>100?sign1.substring(0,100):sign1;
        System.out.println("sign"+sign);
        response.setHeader("sign",sign);
        return json;
    }

    // 还款接口是直连接口，不能通过页面表单提交到控制层进行异步回调

}
